package cn.choujay.tlias.controller;

import cn.choujay.tlias.common.Result;
import cn.choujay.tlias.domain.Dept;
import cn.choujay.tlias.domain.Emp;
import cn.choujay.tlias.service.EmpService;
import cn.choujay.tlias.utils.COSUtil;
import cn.choujay.tlias.utils.JWTUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Slf4j
@CrossOrigin(allowCredentials = "true", exposedHeaders = "token")
@RestController
@RequestMapping("/emp")
public class EmpController {
    @Resource
    private JWTUtil jwtUtil;
    @Resource
    private EmpService empService;
    @Resource
    private COSUtil cosUtil;

    @GetMapping("/login")
    public Result Login(@RequestParam(value = "username") String username, @RequestParam(value = "password") String password) {
        System.out.println(username + "......." + password);
        Emp byIdEmp = empService.getByIdEmp(username);
        System.out.println(byIdEmp);
        if (byIdEmp != null) {
            if (password.equals(byIdEmp.getPassword())) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("id", byIdEmp.getId());
                map.put("username", byIdEmp.getUsername());
                String jwt = jwtUtil.genJwt(map);
                return Result.success(jwt);
            } else {
                return Result.error("密码错误");
            }
        } else {
            return Result.error("此用户不存在");
        }

    }


    @GetMapping("/check")
    public Result CheckUsername(@RequestParam(value = "username") String username) {
        System.out.println(username + ".......");
        Emp byIdEmp = empService.getByIdEmp(username);
        System.out.println(byIdEmp);
        return Result.success();

    }

    @RequestMapping("/logout")
    //HttpServletRequest 是这个类
    public Result Logout(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if ("user".equals(cookie.getName())) {
                cookie.setMaxAge(0);
            }
        }
        return Result.success();
    }

    //分页查询 联查
    @GetMapping("/emps")
    public Result getAllEmp(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        System.out.println("currentPage = " + currentPage);
        System.out.println("pageSize = " + pageSize);
        PageHelper.startPage(currentPage, pageSize);
        List<Emp> allEmp = empService.getAllEmp();
        PageInfo<Emp> pageInfo = new PageInfo<>(allEmp);
        return Result.success(pageInfo);
    }

    @PostMapping("/add")
    public Result addEmp(@RequestBody Emp emp) {
        System.out.println(emp);
        Boolean flag = empService.addEmp(emp);
        return Result.success(flag);
    }

    //模糊查询
    @GetMapping("/search")
    public Result searchEmp(String name, Integer gender, @DateTimeFormat(pattern = "yyyy-MM-dd") Date start, @DateTimeFormat(pattern = "yyyy-MM-dd") Date end) {
//        System.out.println(name + "......." + gender + "......." + start + "......." + end);
        List<Emp> search = empService.Search(name, gender, start, end);
        return Result.success(search);

    }

    //根据ids 删除员工
    @DeleteMapping("/delete")
    public Result deleteEmp(Integer[] ids) {
        System.out.println(Arrays.toString(ids));
        return empService.deleteEmp(ids) ? Result.success() : Result.error("删除失败");
    }

    //上传接口
    @PostMapping("/upload")
    public Result upload(@RequestParam(name = "image", required = false) MultipartFile image) throws IOException {
        System.out.println("image  ............." + image);
        String url = cosUtil.Upload(image);
        log.info("url: {}", url);

        return Result.success(url);
    }




}
